﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace MedicWave.Visualization3d
{
    public partial class ScatterplotSettingsForm : Form
    {
        //Color originalBackColor;
        //int nColors = 0;
        public ScatterplotSettingsForm()
        {
            InitializeComponent();
        }

        internal DialogResult ShowDialog(ScatterplotSettings settings, bool isBiplot)
        {
            txtBoxPlotTitle.Text = settings.Title;
            txtBoxPlotSubtitle.Text = settings.Subtitle;
            txtBoxXlabel.Text = settings.XLabel;
            txtBoxYlabel.Text = settings.YLabel;
            txtBoxZlabel.Text = settings.ZLabel;

            chckBoxGrid.Checked = settings.ShowGrid;
            chckBoxNames.Checked = settings.ShowCaseNames;
            chckBoxShowLegend.Checked = settings.ShowLegend;
            if( settings.PlotProjectionType == ProjectionType.Orthographic)
                CheckBoxOrthographic.Checked = true;
            else
                CheckBoxOrthographic.Checked = false;

            txtboxCorrThresh.Text = settings.CorrelationThreshold.ToString();
            txtboxCorrThresh.KeyPress += new KeyPressEventHandler(txtboxRtFrom_KeyPress);
            txtboxCorrThresh.Leave += new EventHandler(txtboxCorrThresh_Leave);
            ScrBrCorThr.ValueChanged += new EventHandler(ScrBrCorThr_ValueChanged);

            CheckBoxPrinterColors.Checked = settings.PrinterFriendlyColors;
            if( isBiplot)
            {
                groupBoxBiPlot.Enabled = true;
            }
            else
            {
                groupBoxBiPlot.Enabled = false;
                groupBoxBiPlot.Visible = false;
                //buttOk.Location = new Point(buttOk.Location.X, 286);
                //buttOk.Location.Y = 286;
                //buttCancel.Location.Y = 286;
                //buttCancel.Location = new Point(buttCancel.Location.X, 286);
                //btnDefault.Location = new Point(btnDefault.Location.X, 286);
                //btnDefault.Location.Y = 286;
                this.Size = new Size(this.Size.Width, 340);
            }
            this.MinimumSize = this.Size;
            this.MaximumSize = this.Size;
            
            bkgColorButton.BackColor = settings.BackgroundColor;
            chckBoxPlanes.Checked = settings.DrawInnerPlanes;
            return base.ShowDialog();
        }

        void txtboxCorrThresh_Leave(object sender, EventArgs e)
        {
            float value = (float)System.Convert.ToDouble(txtboxCorrThresh.Text);
            if(value > 1.0f)
            {
                value = 1.0f;
            }
            else if(value < 0.0f)
            {
                value = 0.0f;
            }
            txtboxCorrThresh.Text = value.ToString();
        }

        void ScrBrCorThr_ValueChanged(object sender, EventArgs e)
        {
            float value = (float) System.Convert.ToDouble(txtboxCorrThresh.Text);
            if( ScrBrCorThr.Value == -1)
            {
                value += 0.1f;
                if( value > 1)
                {
                    value = 1.0f;
                }
            }
            else if(ScrBrCorThr.Value == 1)
            {
                value -= 0.1f;
                if( value< 0)
                {
                    value = 0.0f;
                }
            }
            ScrBrCorThr.Value = 0;
            txtboxCorrThresh.Text = value.ToString();
        }

        /// <summary>
        /// Event that checks input that provides only numeric data
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void txtboxRtFrom_KeyPress(object sender, KeyPressEventArgs e)
        {
            string keyInput = e.KeyChar.ToString();
            System.Globalization.NumberFormatInfo numberFormatInfo = System.Globalization.CultureInfo.CurrentCulture.NumberFormat;
            string decimalSeparator = numberFormatInfo.NumberDecimalSeparator;
            string groupSeparator = numberFormatInfo.NumberGroupSeparator;
            string negativeSign = numberFormatInfo.NegativeSign;

            if (Char.IsDigit(e.KeyChar))
            {
                base.OnKeyPress(e);
                e.Handled = false;
            }
            else if (keyInput.Equals(decimalSeparator) || keyInput.Equals(groupSeparator) ||
         keyInput.Equals(negativeSign))
            {
                base.OnKeyPress(e);
                e.Handled = false;
            }
            else if (e.KeyChar == '\b')
            {
                base.OnKeyPress(e);
                e.Handled = false;
            }
            else if ((ModifierKeys & (Keys.Control | Keys.Alt)) != 0)
            {
                base.OnKeyPress(e);
                e.Handled = false;
            }
            else
            {
                // Consume this invalid key and beep
                e.Handled = true;
                //MessageBeep();
            }
        }

        private void btnDefault_Click(object sender, EventArgs e)
        {
            bkgColorButton.BackColor = Color.FromArgb(255, 40, 40, 40);
            chckBoxGrid.Checked = true;
            chckBoxNames.Checked = true;
            chckBoxShowLegend.Checked = true;
            CheckBoxOrthographic.Checked = true;
            CheckBoxPrinterColors.Checked = false;            
        }

        public float GetCoorelationThreshold()
        {
            return (float)System.Convert.ToDouble(txtboxCorrThresh.Text);
        }

        public Color GetBackGroundColor()
        {
            return bkgColorButton.BackColor;
        }

        private void buttOk_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.OK;
        }

        private void buttCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
        }

        public bool GetShowGridSet()
        {
            return chckBoxGrid.Checked;
        }

        public ProjectionType GetOrtographicProjSet()
        {
            if (CheckBoxOrthographic.Checked)
                return ProjectionType.Orthographic;
            else
                return ProjectionType.Perspective;
        }

        public bool GetPrinterFriendlSet()
        {
            return CheckBoxPrinterColors.Checked;
        }

        private void bkgColorButton_Click(object sender, EventArgs e)
        {
            if (colorDialogCtrl.ShowDialog() == DialogResult.OK)
            {
                // Set color of button
                bkgColorButton.BackColor = colorDialogCtrl.Color;
            }
        }

        public string GetTitle()
        {
            return txtBoxPlotTitle.Text;
        }
        public string GetSubtitle()
        {
            return txtBoxPlotSubtitle.Text;
        }
        public string GetXLabel()
        {
            return txtBoxXlabel.Text;
        }
        public string GetYLabel()        
        {
            return txtBoxYlabel.Text;
        }
        public string GetZLabel()
        {
            return txtBoxZlabel.Text;
        }

        public bool GetShowLegend()
        {
            return chckBoxShowLegend.Checked;
        }

        public bool GetShowNames()
        {
            return chckBoxNames.Checked;
        }

        internal bool GetDrawInnerPlanes()
        {
            return chckBoxPlanes.Checked;
        }
    }
}